import FYLog from "./FYLog";

const { ccclass, property } = cc._decorator;
/** 自己封装的Node基类 */
@ccclass
export default class FYBaseNode extends cc.Component {

    public setWorldPostion(pos: cc.Vec2) {
        // 改变世界坐标，同时也要更新本地坐标
        let vec3Pos = this.node.parent.convertToNodeSpaceAR(pos)
        this.node.setPosition(new cc.Vec2(vec3Pos.x, vec3Pos.y));
    }

    public getWorldPosition() {
        return this.node.convertToWorldSpaceAR(new cc.Vec2(0, 0));
    }

    /**
     * 获取在指定目标对象里面的坐标
     * @param target 目标对象
     */
    public getInTargetSpacePos(target: cc.Node) {
        if (target instanceof cc.Node) {
            return target.parent.convertToNodeSpaceAR(this.getWorldPosition());
        }

        FYLog.error("target is null");

        return new cc.Vec2(0, 0);
    }

    /**
     * 添加监听
     */
    protected addListener() {

    }

    /**
     * 移除监听
     */
    protected removeListener() {

    }

    /* ------------------- 回调 ---------------------- */

    /* -------------------生命周期--------------------- */

    protected onEnable() {
        this.addListener();
    }

    protected onDisable() {
        this.removeListener();
    }
}
